/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.samples;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.*;
import org.balisunrise.annotation.*;
import org.hibernate.annotations.Type;

/**
 *
 * @author Glauco
 */
@Entity
// <editor-fold defaultstate="collapsed" desc="VisualEntity">
@VisualEntity(transactions = {
    @Transaction(alias = "def", editor = "def", search = "def", title = "Cadastro de Cidades")},
editors = {
    @Editor(alias = "def", panel = "def", title = "Cidade")},
searchs = {
    @Search(alias = "def", filter = "def", grid = "def", title = "Pesquisa de Cidades")},
panels = {
    @Panel(alias = "def", title = "Dados da Cidade"),
    @Panel(alias="sel", title="Cidade")},
filters = {
    @Filter(alias = "def",title="Filtrar")},
grids = {
    @Grid(alias = {"def","emb"}, height = 240, title="Estados")})// </editor-fold>
public class Cidade implements Serializable{

    // <editor-fold defaultstate="collapsed" desc="PropertyChangeSupport">
    @Transient
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
    public static final String PROP_ID = "id";
    public static final String PROP_NOME = "nome";
    public static final String PROP_UF = "uf";
    public static final String PROP_ESTADO = "estado";
    public static final String PROP_IBGE = "ibge";
    // </editor-fold>
    
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    private Integer id;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 50, columnDefinition = "char(50)")
    @Type(type = "org.balisunrise.dao.hibernate.TrimmedString")
    @Selection(aliasForFields="sel",filterType=FilterType.ILIKE)
    @PanelField(definitions = {
        @FieldDefinition(alias={"def","sel"},label = "Cidade")})
    @FilterCriteria(definitions = {
        @CriteriaDefinition(alias="def",label = "Cidade", filterType = FilterType.ILIKE)})
    @GridColumn(definitions = {
        @ColumnDefinition(alias={"def","emb"},editable = false, name = "Cidade")})
    // </editor-fold>
    private String nome;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @ManyToOne
    @PanelField(
        selection = {@FieldSelection(alias="def",
                notFoundMessage = "Estado não encontrado!",
                panel = "selByUF", search = "")},
        embedded={@FieldEmbedded(alias="sel",panel="sel")})
    @GridColumn(embedded = {
        @ColumnEmbedded(alias={"def","emb"}, grid = "embUF", index = 2)})
    // </editor-fold>
    private Estado estado;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length=7)
    @PanelField(definitions = {
        @FieldDefinition(alias = "def", label = "IBGE")})
    @GridColumn(definitions = {
        @ColumnDefinition(alias = "def", editable = false, name = "IBGE")})// </editor-fold>
    private Integer ibge;

    // <editor-fold defaultstate="collapsed" desc="Gets & Sets">
    public Integer getIbge() {
        return ibge;
    }

    public void setIbge(Integer ibge) {

        if (ibge == null || ibge.toString().length() != 7)
            throw new RuntimeException(
                    "Código do IBGE deve conter sete (7) dígitos.");

        Integer oldIbge = this.ibge;
        this.ibge = ibge;
        propertyChangeSupport.firePropertyChange(PROP_IBGE, oldIbge, ibge);
    }

    public Estado getEstado() {
        return estado;
    }

    /**
     * Ao atribuir o estado, a UF do estado é atribuída a UF desta cidade.
     * @param estado
     */
    public void setEstado(Estado estado) {

        if(estado == null)
            throw new RuntimeException("Estado deve ser informado!");

        Estado oldEstado = this.estado;
        this.estado = estado;
        propertyChangeSupport.firePropertyChange(PROP_ESTADO, oldEstado, estado);
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {

        if(nome == null || nome.length() == 0)
            throw new RuntimeException("Nome da Cidade deve ser informado!");

        String oldNome = this.nome;
        this.nome = nome;
        propertyChangeSupport.firePropertyChange(PROP_NOME, oldNome, nome);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        Integer oldId = this.id;
        this.id = id;
        propertyChangeSupport.firePropertyChange(PROP_ID, oldId, id);
    }
    // </editor-fold>
}
